/**
 * 
 */
package sistema.submissao.submissaoChain;

import java.io.File;
import java.io.FileInputStream;
import java.io.PrintWriter;

import org.apache.commons.chain.Command;
import org.apache.commons.chain.Context;

import sistema.contexto.problemas.ProblemaIF;
import sistema.contexto.regras.Result;
import sistema.submissao.util.SubmissaoFileUtils;
import sistema.validador.Diff;

/**
 * @author Thiago Emmanuel Pereira da Cunha Silva, thiago.manel@gmail.com
 * @since 03/05/2006
 */
public class ValidationCommand implements Command {

	/* (non-Javadoc)
	 * @see org.apache.commons.chain.Command#execute(org.apache.commons.chain.Context)
	 */
	public boolean execute(Context context) throws Exception {
		ProblemaIF problema = (ProblemaIF) context.get("problema");
		String pathSaida = problema.getPathSaida();
		String saidaSubmissao = (String) context.get("fileSaida");
		
		Result result = (Result) context.get("result");
		Diff diff = new Diff();
		
		PrintWriter log = new PrintWriter("log");
		
		if (diff.diffone(saidaSubmissao, pathSaida, log) == 1) {
			result.setValidationStatus(Result.Outputs.SUCESS);
			return false;
		} else {
			result.setValidationStatus(Result.Outputs.ERROR);
			File logFile = new File("log");
			String saidaLog = SubmissaoFileUtils.trataStream(new FileInputStream(
					 logFile));
			return true;
		}
	}

}
